﻿Imports System.Data.Linq
Imports System.Linq
Public Class CHblExchangesController
    Dim objDataContext As New TanNamChinhDataContext
    Public Function GetHblExchange(ByVal idHBL As Integer) As ISingleResult(Of sp_Thanh_GetSellingCurrListResult)
        Try
            Return objDataContext.sp_Thanh_GetSellingCurrList(idHBL)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function CheckExistHblExchanges(ByVal idHBL As Integer) As Boolean
        Return objDataContext.tblHBLExchanges.Any(Function(s) s.ID_HBL = idHBL)
    End Function
    Public Function GetRatioOfCur(ByVal _idCurrency As Integer, ByVal _idHBL As Integer) As Double
        Try
            Return (From h In objDataContext.tblHBLExchanges
                    Where h.ID_Currency = _idCurrency And h.ID_HBL = _idHBL
                    Select h).Single.Ratio
        Catch ex As Exception
            Return 1
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblHBLExchange) As Integer
        Try
            objDataContext.tblHBLExchanges.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            Return LocalTable.ID_HBLExchange
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblHBLExchange)
        Try
            objDataContext.tblHBLExchanges.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblHBLExchange)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblHBLExchanges.DeleteOnSubmit( _
              (From r In objDataContext.tblHBLExchanges _
               Where r.ID_HBLExchange = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub DeleteAllRecord(ByVal _idHBL As Integer)
        Try
            objDataContext.tblHBLExchanges.DeleteAllOnSubmit( _
              (From r In objDataContext.tblHBLExchanges _
               Where r.ID_HBL = _idHBL))
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub New()
        objDataContext = New TanNamChinhDataContext
    End Sub
    Public Sub New(ByVal _obj As TanNamChinhDataContext)
        objDataContext = _obj
    End Sub
    Public Sub RefreshData()
        objDataContext = New TanNamChinhDataContext
    End Sub
End Class
